package com.yy.micro.magic.core.model;

import com.yy.micro.magic.core.config.Content;

import org.springframework.util.StringUtils;

import java.util.Objects;
 /**
   * @author YY
   * @version 1.0.0
   * @update 2021/9/22 9:37 下午
   * @description: 数据变换中数据节点的描述。
 */
public class RuleKey {

    private String name;
    private Integer index;
    private RuleKey parent;

    public RuleKey(String key){
        if (!StringUtils.isEmpty(key)){
            if (key.contains(Content.KEY_PATH_SPERALTOR)){
                this.name = key.substring(key.lastIndexOf(Content.KEY_PATH_SPERALTOR)+1);
                String str = key.substring(0,key.lastIndexOf(Content.KEY_PATH_SPERALTOR));
                if (name.contains(Content.KEY_LIST_INDENTIFIER)){
                    String num = name.substring(name.lastIndexOf(Content.KEY_LIST_INDENTIFIER)+1);
                    name = name.substring(0,name.lastIndexOf(Content.KEY_LIST_INDENTIFIER));
                    index = Integer.valueOf(num);
                }
                this.parent = new RuleKey(str);
            }else{
                this.name = key;
            }
        }else {
            throw  new IllegalArgumentException("Key is empty");
        }
    }

    public RuleKey(RuleKey parent,String name,Integer index){
        this.parent = parent;
        this.name = name;
        this.index = index;
    }

    public String getKey() {
        StringBuilder sb = new StringBuilder();
        if (null != parent){
            sb.append(parent.getKey());
            sb.append(Content.KEY_PATH_SPERALTOR);
        }
        sb.append(name);
        if (null != index && index >-1){
            sb.append(Content.KEY_LIST_INDENTIFIER);
            sb.append(index);
        }
        return sb.toString();
    }

    public String getName() {
        return name;
    }

    public void syncKeySequence(RuleKey target){
        if (null != target){
            RuleKey sk = findSequenceKey();
            if (null != sk ){
                RuleKey tk = target.findSequenceKey();
                if (null != tk){
                    sk.index = tk.index;
                    if (null!=sk.parent){
                        sk.parent.syncKeySequence(tk.parent);
                    }
                }
            }
        }
    }

    private RuleKey findSequenceKey(){
        if (null != index && index >-1){
            return this;
        }
        if (null == parent){
            return null;
        }
        return this.parent.findSequenceKey();
    }

//    public String getPath() {
//        return path;
//    }

    public Integer getIndex() {
        return index;
    }

    public RuleKey resetName(String name){
        return RuleKey.create(parent,name,index);
    }

    public boolean equalsPath(RuleKey key,boolean ignoreNum){
        if (ignoreNum){
            return this.equals(key);
        }
        return this.equals(key) && this.index == key.index;
    }

    public RuleKey clone(){
        return create(parent,name,index);
    }

    public static RuleKey create(RuleKey parent,String name){
        return create(parent,name,null);
    }
    public static RuleKey create(RuleKey parent,String name,Integer index){
        return new RuleKey(parent,name,index);
    }

    public static RuleKey create(String key){
        return new RuleKey(key);
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        final RuleKey ruleKey = (RuleKey) o;
        return name.equals(ruleKey.name) && parent.equals(ruleKey.parent);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name,parent);
    }
}
